const path = require('path');
const fs = require('fs');
function imgUpload(req, res) {
    // 业务执行
    let file = req.files[0];
    //获取扩展名
    let extname = path.extname(file.originalname);
    let newPath = path.format({
        root: file.path,
        base: '',
        ext: extname,
    });
    // 重命名
    try {
        fs.rename(file.path, newPath, (err) => {
            if (err) throw err;
        })
        res.send({
            code: 0, msg: "图片上传成功",
            path: path.format({
                root: "http://localhost:3000",
                base: newPath.replace('public', ''),
                ext: 'ignored'
            })
        })
    } catch (e) {
        console.log(e);
    }
}
module.exports = { imgUpload };